﻿Shader "Unlit/Radius Blur"
{
    Properties
    {
        [HideInInspector]_MainTex ("Texture", 2D) = "white"
    }

    SubShader
    {
        Tags
        {
            "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline"
        }

        HLSLINCLUDE
        #pragma vertex vert
        #pragma fragment frag

        #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

        struct Attributes
        {
            float4 positionOS : POSITION;
            float2 uv : TEXCOORD0;
        };

        struct Varyings
        {
            float4 positionHCS : SV_POSITION;
            float2 uv : TEXCOORD0;
        };


        TEXTURE2D(_MainTex);
        SAMPLER(sampler_MainTex);

        CBUFFER_START(UnityPerMaterial)
        float4 _MainTex_ST;
        float4 _MainTex_TexelSize;
        CBUFFER_END
        int _BlurStrength;


        Varyings vert(Attributes IN)
        {
            Varyings OUT;
            OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
            OUT.uv = TRANSFORM_TEX(IN.uv, _MainTex);
            return OUT;
        }
        ENDHLSL

        Pass
        {
            Name "VERTICAL RADIUS BLUR"
            HLSLPROGRAM
            half4 frag(Varyings IN) : SV_TARGET
            {
                return SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, IN.uv);
            }
            ENDHLSL
        }
        
        Pass
        {
            Name "HORIZONTAL RADIUS BLUR"
            HLSLPROGRAM
            half4 frag(Varyings IN) : SV_TARGET
            {
                float2 res = _MainTex_TexelSize.xy;
                half4 sum = 0;
                
                float2 dir = float2(0.5,0.5) - IN.uv ;
                int samples = _BlurStrength+1;
                for (int j = 0; j < samples; ++j)
                {
                    float2 uv = IN.uv - res * dir * j;

                    sum += SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, uv);
                }
                return sum/samples;
            }
            ENDHLSL
        }

    }
}